<?php
/**
 * @author: liaofei<136327134@qq.com>
 * @day: 2021/6/4
 */

namespace App\Dao\system;
use App\Exceptions\ApiException as HOException;
use App\Dao\BaseDao;
use App\Models\system\SystemAdminModel;
use Hlcreation\Basic\Helper;

/**
 * Class SystemAdminDao
 * @package app\dao\system
 */
class SystemAdminDao extends BaseDao
{
   
    public function setModel(){
        return SystemAdminModel::class;
    }
    
   
    /**
     * 用管理员名查找管理员信息
     * @param string $account
     */
    public function accountByAdmin(string $account)
    {
      return  $this->models::where(['account'=>$account])->select('*')->first();
       
    }
    
    
    /**
     * 判断账号是否存在
     */
    public  function  IsAccount(string $account,string $id){
        return $this->models::where(['account'=>$account])->where('id','!=',$id)->count();
    }



    //修改密码
    public function updatePass($uid,$data){
        $adminInfo = $this->models::where('id',$uid)->first();

        if (empty($data['newPass'])){
            throw new HOException("新密码不能为空、请重新填写!");
        }

        if (empty($data['newsPass'])){
            throw new HOException("确认密码不能为空、请重新填写!");
        }

        if($adminInfo->pwd != Helper::returnPwd($data['oldPass'], $adminInfo->halt)){
            throw new HOException("原密码错误、请重新填写!");
        }

        if($data['newPass'] != $data['newsPass']){
            throw new HOException("两次密码输入不一致!");
        }

        $this->models::where(['id'=>$adminInfo->id])->update([
            'pwd'=> Helper::returnPwd($data['newPass'], $adminInfo->halt),
        ]);

        return true;
    }


    //获取角色Id
    public function get_roleId($userId){

        return $this->models::where('id',$userId)->value('roles');
    }





}
